<template>
	<view class="content">
		<view class="tabT">
			<view style="display: flex;flex-direction: row;flex: 1;">
				<view class="items">
					<view class="title" :class="pagination.type=='02'?'active':''" @click="bankChange('02')">
						信用卡
					</view>
				</view>
				<view class="items">
					<view class="title" :class="pagination.type=='00'?'active':''" @click="bankChange('00')">
						储蓄卡
					</view>
				</view>
			</view>

			<view class="editOpen" @click="editFn">
				<u-icon color="#fff" name="setting-fill"></u-icon> <text style="margin-left: 10rpx;">{{editText}}</text>
			</view>
		</view>
		<view class="tabD">
			<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:down="downOption" :up="upOption">
				<view class="items" v-for="(item,i) in list" :key="i">
					<view class="bankInfo">
						<image :src="item.bank_logo" mode="aspectFill"></image>
						<view class="r">
							<view class="h3">
								{{item.name}}
								<view class="p">
									{{item.type=='02'?'信用卡':'储蓄卡'}}
								</view>
							</view>

							<view class="num">
								**** **** **** {{item.full_card_number.substring(item.full_card_number.length-4)}}
							</view>
						</view>
					</view>
					<view class="tool" v-if="edit">
						<view class="toolItem" @click="editBank(i)">
							编辑
						</view>
						<view class="toolItem" @click="bankDel(item.id,i)">
							删除
						</view>
					</view>
				</view>
				<navigator url="/pages/bankcard/creditCard" class="addCard" v-if="pagination.type=='02'">
					<u-icon name="plus"></u-icon>
					添加信用卡
				</navigator>
				<navigator url="/pages/bankcard/debitCard" class="addCard" v-else>
					<u-icon name="plus"></u-icon>
					添加储蓄卡
				</navigator>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	import {
		bankcard,
		bankcardDel,
	} from '../../common/bankcard.api.js'
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				activeIndex: 0,
				edit: false,
				editText: '管理',
				pagination: {
					current: 1,
					page: 1,
					pageSize: 0,
					total: 0,
					type: '02'
				},
				upOption:{},
				downOption:{},
				list: []
			}
		},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			console.log('下拉加载')
		},
		onLoad() {
			let that = this
			uni.$on('updateBankCardInfo', function(data) {
				that.pagination.current = 1
				that.pagination.type = data.type
				that.getList()
			})
		},
		methods: {
			editFn() {
				this.edit = !this.edit

				if (this.editText == '管理') {
					this.editText = '完成'
				} else {
					this.editText = '管理'
				}
			},
			getList() {
				let that = this;
				let params = {}
				params.page = that.pagination.current;
				params.type = that.pagination.type;
				params.page_size = 20
				bankcard(params).then(res => {
					console.log(res)
					if (!res.list) {
						res.list = []
					}
					if (!res.pagination) {
						res.pagination = {}
						res.pagination.total = 0
					}
					that.mescroll.endByPage(res.list.length, res.pagination.total)
					that.pagination.page = that.pagination.current
					if (that.pagination.current * 1 == 1) {
						that.list = res.list
					} else {
						that.list = [...that.list, ...res.list]
					}
					if (that.pagination.type == '02') {
						uni.setStorageSync('bankList', JSON.stringify(that.list))
					}
					res.pagination.type = that.pagination.type;
					that.pagination = res.pagination;
					that.pagination.current = that.pagination.current * 1 + 1
					that.pagination.page = that.pagination.current * 1 + 1
				})
			},
			bankChange(e) {
				this.pagination.type = e;
				this.pagination.page = 1;
				this.pagination.current = 1;
				this.list = []
				this.mescroll.scrollTo(0)
				this.mescroll.setPageNum(1)
				this.mescroll.resetUpScroll();
			},
			upCallback() {
				this.getList()
			},
			downCallback() {
				this.pagination.page = 1;
				this.pagination.current = 1;
				this.mescroll.resetUpScroll();
			},
			showCalendar() {
				this.showCalendarStatus = true
			},
			// 银行卡删除
			bankDel(e, index) {
				console.log(e, index);
				// delBank
				let params = {};
				params.id = e
				bankcardDel(params).then(res => {
					console.log(res)
					uni.showToast({
						icon: 'none',
						title: '删除成功'
					});
					this.list.splice(index, 1)
				})
			},
			openPage(path, params = {}) {
				this.$u.route({
					url: path,
					params
				})
			},
			editBank(e) {
				let that = this;
				this.list[e].index = e
				let params = {
					deCode: {}
				};
				params.deCode = JSON.stringify(this.list[e])
				if (this.pagination.type == '00') {
					this.openPage('/pages/bankcard/debitCard', params)
				} else {
					this.openPage('/pages/bankcard/creditCard', params)
				}
			},

		}
	}
</script>
<style>
	page {
		background-color: #f4f5f7;
	}
</style>
<style scoped lang="scss">
	.tabT {
		display: flex;
		background-color: #ec6a38;
		justify-content: space-between;
		height: 80rpx;

		.editOpen {
			// position: absolute;
			// right: 0;
			// top: 0;
			// height: 100%;
			display: flex;
			flex-direction: row;
			line-height: 80rpx;
			padding: 0 30rpx;
			color: #fff;
		}

		.items {

			width: 30%;
			text-align: center;
			color: #fff;
			display: flex;
			align-items: center;
			justify-content: center;

			.title {
				position: relative;
				height: 100%;
				line-height: 80rpx;
				color: #f7d8cd;

				&::after {
					position: absolute;
					left: 0;
					bottom: 0;
					height: 3px;
					background-color: transparent;
					content: '';
					display: block;
					width: 100%;
				}
			}

			.title.active {
				color: #fff;

				&::after {
					background-color: #fff;
				}
			}
		}
	}

	.tabD {
		padding: 25rpx;

		.tool {
			display: flex;
			margin-left: 120rpx;
			font-size: 26rpx;
			color: #fff;

			.toolItem {
				padding: 10rpx 0;
				margin-right: 50rpx;
			}
		}

		.items {
			background-color: #d3524c;
			border-radius: 10rpx;
			margin-bottom: 25rpx;
			padding: 25rpx;
		}

		.bankInfo {
			display: flex;
			align-items: flex-start;
			font-size: 26rpx;

			image {
				width: 108rpx;
				height: 108rpx;
				display: block;
				margin-right: 15rpx;
				border-radius: 100%;
			}

			.r {
				flex: 1;
				height: 108rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
			}

			.h3 {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-bottom: 10rpx;
			}

			.p {}

			color: #fff;
		}
	}

	.addCard {
		display: flex;
		flex-direction: row;
		height: 80rpx;
		background-color: #fff;
		border-radius: 10rpx;
		line-height: 80rpx;
		padding: 0 25rpx;
		font-size: 28rpx;
	}
</style>
